<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui/lib/theme-chalk/index.css">
    <style>
        /* 引入Element UI字体文件 */
        @font-face {
            font-family: "element-icons";
            src: url('https://cdn.jsdelivr.net/npm/element-ui/lib/theme-chalk/fonts/element-icons.woff') format('woff'),
            url('https://cdn.jsdelivr.net/npm/element-ui/lib/theme-chalk/fonts/element-icons.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f2f5;
            font-family: 'Arial', sans-serif;
        }

        .login-container {
            width: 400px;
            padding: 20px;
            background-color: #fff;
            border-radius: 4px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .login-form {
            margin-top: 20px;
        }

        .login-button {
            width: 100%;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/element-ui@2.15.1/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app" class="login-container">
    <el-form :model="loginForm" ref="loginForm" label-width="80px" class="login-form">
        <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
            <el-input v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
            <el-input type="password" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button class="login-button" type="primary" @click="submitForm('loginForm')">登录</el-button>
        </el-form-item>
    </el-form>
</div>

<script>
    new Vue({
        el: '#app',
        data: function() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.get('/login', {
                            params: {
                                operation: 'login',
                                username: this.loginForm.username,
                                password: this.loginForm.password
                            }
                        })
                            .then(response => {
                                if (response.data.code === 200) {
                                    this.$message({
                                        message: '登录成功',
                                        type: 'success'
                                    });
                                    if (this.loginForm.username==='admin'){
                                        location.href = '/admin.html';
                                    }else {
                                        localStorage.setItem('userId',response.data.data.userId)
                                        location.href = '/front.html';
                                    }

                                    // Handle successful login logic here, e.g., redirecting to another page
                                } else if (response.data.code === 401) {
                                    this.$message.error('用户名或密码错误');
                                } else {
                                    this.$message.error('登录失败');
                                }
                            })
                            .catch(error => {
                                this.$message.error('请求失败');
                                console.error(error);
                            });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    });
</script>
</body>
</html>
